package com.rp.system.dao;

import java.util.List;

import org.raptor.common.DbOperUtil;
import org.raptor.orm.hibernate.DbaHibernateTemplateSupport;
import org.raptor.pagination.Page2;
import org.springframework.stereotype.Repository;

import com.rp.common.vo.PageParam;
import com.rp.system.po.Menu;

@Repository
public class MenuDao extends DbaHibernateTemplateSupport<Menu> {
	
	public static final String INSERT_MENU = "INSERT INTO RP_SYSTEM_MENUS(id,createDate,updateDate,version,name,iconCls,title,url,enabled2) VALUES(?,?,?,?,?,?,?,?,?)";
	
	/**
	 * 根据名称获取菜单
	 * @param name
	 * @return
	 */
	public Menu getMenuByName(String name){
		String hql = "from Menu where name = ?";
		List<Menu> menus = this.find(hql, name);
		if(menus.size() > 0){
			return menus.get(0);
		}
		return null;
	}
	
	/**
	 * 分页获取菜单数据
	 * @param pageParam
	 * @return
	 */
	public Page2 getMenusByPage(PageParam pageParam){
		String hql = "from Menu";
		return this.getPage2ByOffset(hql, 
				pageParam.getPageOffset(), pageParam.getPageSize(),
				pageParam.getSortField(), pageParam.getSortOrder());
	}
	
	/**
	 * 获取所有菜单数据，按时间升序排列
	 * @param enabled2 激活状态：1-已激活，0-未激活
	 * @return
	 */
	public List<Menu> getAllMenus(String enabled2){
		if(enabled2 == null || enabled2.equals("")){
			return this.find("from Menu"+DbOperUtil.generateOrderBy("createDate", "asc"));
		}
		String hql = "from Menu where enabled2 = ?"+DbOperUtil.generateOrderBy("createDate", "asc");
		return this.find(hql, enabled2);
	}
	
}
